//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef __AURA_PRIVATE_H_
#define __AURA_PRIVATE_H_

#include "aura.h"
#include "wince.h"
#include "linknode.h"

#define THIS(objptr, type, member) \
        ((type *)((char *)(objptr) - offsetof(type, member)))

#ifdef _GNUC
#define AuraDebugBreak()    do { __asm("int $3"); } while(0)
#elif defined(_MSC_VER)
#define AuraDebugBreak()  //  do { __asm int 3 } while(0)
#endif // _x86

#undef assert
#if defined(_DEBUG)
void __cdecl _assert(const char *exp, const char *fn, int ln);
#define assert(exp)  do { \
                        if (exp) { \
                            (void)0; \
                        } else { \
                            _assert(#exp, __FILE__, __LINE__); \
                            AuraDebugBreak(); \
                        } \
                     } while(0)
#else
#define assert(exp) ((void) 0)
#endif // _DEBUG

#define IS_LEAP_YEAR(x)   ((x)% 400 == 0 || (!((x) & 3)  \
                           && (x) % 100 != 0))

// --------- for object description definition---
typedef struct _ThreadDescr {
    HANDLE m_hThread;
    DLinkNode m_inWaitList;
    INT m_eventState;   // for event.
    HANDLE m_hEvent;
}ThreadDescr;

typedef struct _MutexDescr {
    struct _ThreadDescr * m_pOwner;
    HANDLE m_hMutex;
    DWORD m_count;
}MutexDescr;

typedef struct _EventDescr {
    struct _ThreadDescr * m_pOwner;
    WIN_CRITICAL_SECTION m_hLock;
    DLinkNode m_waitList;
    Aura_EventState m_eventState;
    Aura_Bool m_bManualReset;
}EventDescr;

typedef struct _CondDescr {
    WIN_CRITICAL_SECTION m_hLock;
    DLinkNode m_waitList;
}CondDescr;

EXTERN DWORD s_threadDescrIndex;
ThreadDescr * GetCurrentThreadDescr();

//-----------------------------------------------
#define DETACHED_INDEX ((DWORD)-1)

typedef struct _ProcessObject {
    DWORD  m_processId;
    HANDLE m_hProcess;
    DWORD  m_index;  // index itself in the array.
} ProcessObject;


// -----------for error code -------------------------------

Aura_ECode Win32ErrorToECodeError(DWORD dwError);
Aura_ECode GetLastECodeError();

// -----------for time convertion ---------------------------
void WinSysTimeConvertToAura(
          /* [in]  */ LPSYSTEMTIME pWinSysTime,
          /* [out] */ Aura_SystemTime * pAuraSysTime);

void AuraSysTimeConvertToWin(
          /* [in]  */ Aura_SystemTime * pAuraSysTime,
          /* [out] */ LPSYSTEMTIME pWinSysTime);

void WinFileTimeConvertToAuraSystime(
          /* [in]  */ LPFILETIME pFileTime,
          /* [out] */ Aura_SystemTime *pAuraSysTime);

void WinFileTimeConvertToMilliseconds(
          /* [in]  */ LPFILETIME pFileTime,
          /* [out] */ Aura_Millisecond32 * pMilliseconds);

// ---------- for priority -----------------------------------

Aura_ThreadPriority WinPriorityConvertToAura(
          /* [in]  */ int priority);

int AuraPriorityConvertToWin(
          /* [in]  */ Aura_ThreadPriority priority);

// ----------- for priroity rank -----------------------------

Aura_ThreadPriorityRank WinPriorityClassConvertToAura(
          /* [in]  */ DWORD dwPriorityClass);

DWORD AuraPriorityClassConvertToWin(
          /* [in]  */ Aura_ThreadPriorityRank prioirtyRank);

// -----------for virtual memory ------------------------------
DWORD AuraMemoryMapFlagsConvertToWin(
           /* [in]  */ Aura_MemoryMapFlags flags);

DWORD AuraMemoryProtectionConvertToWin(
           /* [in]  */ Aura_MemoryProtection protect);

Aura_MemoryProtection WinMemoryProtectionConvertToAura(
           /* [in]  */ DWORD dwProtect);

// ---------- for sharedmemory --------------------------------
DWORD AuraMemoryProtectionConvertToWinFileAccess(
           /* [in]  */ Aura_MemoryProtection protect);


#endif // __AURA_PRIVATE_H_
